home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_std / array.e < prev    next >
Text File  |  2000-03-25  |  9KB  |  338 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. class ARRAY[E]
  13.    -- 
  14.    -- General purpose resizable ARRAYs.
  15.    -- 
  16.  
  17. inherit ARRAYED_COLLECTION[E];
  18.  
  19. creation make, with_capacity, from_collection
  20.  
  21. feature
  22.    
  23.    lower: INTEGER;     
  24.          -- Lower index bound.
  25.    
  26. feature -- Creation and Modification :
  27.    
  28.    make(min_index, max_index: INTEGER) is
  29.          -- Prepare the array to hold values for indexes in range
  30.      -- [`min_index' .. `max_index']. Set all values to default.
  31.          -- When `max_index' = `min_index' - 1, the array `is_empty'.
  32.       require
  33.          valid_bounds: min_index <= max_index + 1
  34.       local
  35.          needed: INTEGER;
  36.       do
  37.          lower := min_index;
  38.          upper := max_index;
  39.          needed := max_index - min_index + 1;
  40.          if needed > 0 then
  41.             if capacity < needed then
  42.                storage := storage.calloc(needed);
  43.                capacity := needed;
  44.             else
  45.                clear_all;
  46.             end;
  47.          end;
  48.       ensure
  49.          lower_set: lower = min_index;
  50.          upper_set: upper = max_index;
  51.          items_set: all_default
  52.       end;
  53.  
  54.    with_capacity(needed_capacity, low: INTEGER) is
  55.          -- Create an empty array with `capacity' initialized
  56.          -- at least to `needed_capacity' and `lower' set to `low'.
  57.       require
  58.          needed_capacity >= 0
  59.       do
  60.          if capacity < needed_capacity then
  61.             storage := storage.calloc(needed_capacity);
  62.             capacity := needed_capacity;
  63.          end;
  64.          lower := low;
  65.          upper := low - 1;
  66.       ensure
  67.          is_empty;
  68.          needed_capacity <= capacity;
  69.          lower = low
  70.       end;
  71.  
  72. feature -- Modification :
  73.    
  74.    resize(min_index, max_index: INTEGER) is
  75.          -- Resize to bounds `min_index' and `max_index'. Do not lose any 
  76.          -- item whose index is in both [`lower' .. `upper'] and 
  77.          -- [`min_index' .. `max_index']. New positions if any are 
  78.          -- initialized with the appropriate default value.
  79.       require
  80.          min_index <= max_index + 1
  81.       local
  82.          needed, offset, intersize: INTEGER;
  83.       do
  84.          needed := max_index - min_index + 1;
  85.          if needed > 0 then
  86.             if needed > capacity then
  87.                if capacity = 0 then
  88.                   storage := storage.calloc(needed);
  89.                   capacity := needed;
  90.                else
  91.                   storage := storage.realloc(capacity, needed);
  92.                   capacity := needed;
  93.                end;
  94.             end;
  95.             offset := lower - min_index;
  96.             intersize := max_index.min(upper) - min_index.max(lower) + 1;
  97.             if intersize > 0 then
  98.                if offset = 0 then
  99.                   if intersize < needed then
  100.                      storage.clear(intersize, needed - 1);
  101.                   end;
  102.                elseif offset < 0 then
  103.                   storage.move(-offset, intersize - offset - 1, offset);
  104.                   if intersize < needed then
  105.                      storage.clear(intersize, needed - 1);
  106.                   end;
  107.                else
  108.                   storage.move(0, intersize - 1, offset);
  109.                   storage.clear(0, offset - 1);
  110.                   if intersize + offset < needed then
  111.                      storage.clear(intersize + offset, needed - 1);
  112.                   end;
  113.                end;
  114.             else
  115.                storage.clear(0, needed - 1);
  116.             end;
  117.          end;
  118.          lower := min_index;
  119.          upper := max_index;
  120.       ensure
  121.      lower = min_index;
  122.      upper = max_index
  123.       end;
  124.  
  125.    reindex(new_lower: INTEGER) is
  126.      -- Change indexing to take in account the expected `new_lower' 
  127.      -- index. The `upper' index is translated accordingly.
  128.       local
  129.      i: INTEGER;
  130.       do
  131.      i := new_lower - lower;
  132.      lower := lower + i;
  133.      upper := upper + i;
  134.       ensure
  135.      lower = new_lower;
  136.      count = old count
  137.       end;
  138.    
  139.  
  140. feature -- Implementation of deferred :
  141.  
  142.    subarray(min, max: INTEGER): like Current is
  143.       do
  144.      Result := slice(min,max);
  145.      Result.reindex(min);
  146.       ensure then
  147.          Result.lower = min;
  148.       end;
  149.  
  150.    is_empty: BOOLEAN is
  151.       do
  152.          Result := upper < lower;
  153.       end;
  154.  
  155.    count: INTEGER is
  156.       do
  157.          Result := upper - lower + 1;
  158.       end;
  159.  
  160.    item, infix "@" (index: INTEGER): E is
  161.       do
  162.          Result := storage.item(index - lower);
  163.       end;
  164.    
  165.    put(element: like item; index: INTEGER) is
  166.       do
  167.          storage.put(element,index - lower);
  168.       end;
  169.  
  170.    force(element: like item; index: INTEGER) is
  171.       require else
  172.          true
  173.       do
  174.          if upper < index then
  175.             if index = upper + 1 then
  176.                add_last(element);
  177.             else
  178.                resize(lower,index);
  179.                put(element,index);
  180.             end;
  181.          elseif index < lower then
  182.             resize(index,upper);
  183.             put(element,index);
  184.          else
  185.             put(element,index);
  186.          end;
  187.       ensure then
  188.      lower = index.min(old lower)
  189.       end;
  190.  
  191.    copy(other: like Current) is
  192.       local
  193.          needed_capacity: INTEGER;
  194.       do
  195.          lower := other.lower;
  196.          upper := other.upper;
  197.          needed_capacity := upper - lower + 1;
  198.          if capacity < needed_capacity then
  199.             capacity := needed_capacity;
  200.             storage := storage.calloc(capacity);
  201.          end;
  202.          if needed_capacity > 0 then
  203.             storage.copy_from(other.storage,needed_capacity - 1);
  204.          end;
  205.       end;
  206.    
  207.    set_all_with(v: like item) is
  208.       do
  209.          storage.set_all_with(v,upper - lower);
  210.       end;
  211.  
  212.    remove_first is
  213.       do
  214.          storage.remove_first(upper - lower);
  215.          lower := lower + 1;
  216.       ensure then
  217.          upper = old upper;
  218.       end;
  219.    
  220.    remove(index: INTEGER) is
  221.       do
  222.          storage.remove(index - lower,upper - lower);
  223.          upper := upper - 1;
  224.       end;
  225.    
  226.    clear is
  227.       do
  228.          upper := lower - 1;
  229.       ensure then
  230.          capacity = old capacity
  231.       end;
  232.  
  233.    add_first(element: like item) is
  234.       do
  235.          if upper < lower then
  236.             add_last(element);
  237.          else
  238.             add_last(element);
  239.             move(lower,upper - 1,1);
  240.             put(element,lower);
  241.          end;
  242.       end;
  243.  
  244.    add_last(element: like item) is
  245.       local
  246.          new_capacity: INTEGER;
  247.       do
  248.          if capacity < count + 1 then
  249.             if capacity = 0 then
  250.                capacity := 16;
  251.                storage := storage.calloc(capacity);
  252.             else
  253.                new_capacity := 2 * capacity;
  254.                storage := storage.realloc(capacity,new_capacity);
  255.                capacity := new_capacity;
  256.             end;
  257.          end;
  258.          upper := upper + 1;
  259.          put(element,upper);
  260.       end;
  261.  
  262.    from_collection(model: COLLECTION[like item]) is
  263.       local
  264.          i, up: INTEGER;
  265.       do
  266.          from
  267.             with_capacity(model.count,model.lower);
  268.             i := model.lower;
  269.             up := model.upper;
  270.             upper := up;
  271.          until
  272.             i > up
  273.          loop
  274.             put(model.item(i),i);
  275.             i := i + 1;
  276.          end;
  277.       ensure then
  278.          lower = model.lower;
  279.          upper = model.upper
  280.       end;
  281.  
  282.    all_default: BOOLEAN is
  283.       do
  284.          Result := storage.all_default(upper - lower);
  285.       end;
  286.  
  287.    nb_occurrences(element: like item): INTEGER is
  288.       do
  289.          Result := storage.nb_occurrences(element,upper - lower);
  290.       end;
  291.       
  292.    fast_nb_occurrences(element: like item): INTEGER is
  293.       do
  294.          Result := storage.fast_nb_occurrences(element,upper - lower);
  295.       end;
  296.       
  297.    index_of(element: like item): INTEGER is
  298.       do
  299.          Result := lower + storage.index_of(element,upper - lower);
  300.       end;
  301.  
  302.    fast_index_of(element: like item): INTEGER is
  303.       do
  304.          Result := lower + storage.fast_index_of(element,upper - lower);
  305.       end;
  306.  
  307.    is_equal(other: like Current): BOOLEAN is
  308.       do
  309.          if Current = other then
  310.             Result := true;
  311.          elseif lower = other.lower and then upper = other.upper then
  312.             Result := storage.fast_memcmp(other.storage,count);
  313.          end;
  314.       end;
  315.  
  316.    is_equal_map(other: like Current): BOOLEAN is
  317.       do
  318.          if Current = other then
  319.             Result := true;
  320.          elseif lower = other.lower and then upper = other.upper then
  321.             Result := storage.memcmp(other.storage,count);
  322.          end;
  323.       end;
  324.  
  325.    slice(min, max: INTEGER): like Current is
  326.       do
  327.      !!Result.make(lower,lower + max - min);
  328.      Result.storage.copy_slice(0,storage,min - lower,max - lower);
  329.       end;
  330.  
  331.    get_new_iterator: ITERATOR[E] is
  332.       do
  333.          !ITERATOR_ON_COLLECTION[E]!Result.make(Current);
  334.       end;
  335.  
  336. end -- ARRAY[E]
  337.  
  338.